const { Sequelize, DataTypes } = require('sequelize');
const config = require('./config/config.json').development;

// 创建 Sequelize 实例
const sequelize = new Sequelize(config.database, config.username, config.password, {
  host: config.host,
  dialect: config.dialect,
  port: config.port,
});

// 定义客户模型
const Customer = sequelize.define('customer', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false
  },
  organizationId: {
    type: DataTypes.INTEGER,
    allowNull: false
  }
}, {
  tableName: 'customers',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

async function checkCustomers() {
  try {
    // 测试数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');

    // 查询客户数量
    const customerCount = await Customer.count();
    console.log(`客户表中有 ${customerCount} 条记录`);

    // 如果有客户数据，显示前几条
    if (customerCount > 0) {
      const customers = await Customer.findAll({
        limit: 5,
        order: [['created_at', 'DESC']]
      });
      console.log('最近的客户:');
      customers.forEach(customer => {
        console.log(`- 客户ID: ${customer.id}, 姓名: ${customer.name}`);
      });
    } else {
      console.log('客户表中没有数据');
    }

  } catch (error) {
    console.error('检查客户表时出错:', error);
  } finally {
    await sequelize.close();
  }
}

checkCustomers();